#include "pefactory.h"
#include "pe_ccnode.h"
#include "pe_cclayer.h"
#include "pe_cclayercolor.h"
#include "CCQTSceneDirector.h"
#include "pe_ccmenuitemfont.h"
#include "pe_ccmenuitemtoggle.h"
#include "pe_cclabelttf.h"
#include "pe_ccmenuitemsprite.h"
#include "pe_ccsprite.h"
#include "pe_ccparticlesystem.h"
#include "constantManager.h"


PEFactory::PEFactory()
{
}
static PEFactory peFactory;

PEFactory *PEFactory::shareFactory()
{
	if ( !m_bInit)
	{
		m_bInit = true;
		peFactory.init();
	}
    return &peFactory;
}



void PEFactory::init()
{
	m_variantManager = new QtVariantPropertyManager();
	m_variantEditor = new QtTreePropertyBrowser();
	m_variantFactory = new QtVariantEditorFactory();
	m_variantEditor->setFactoryForManager(m_variantManager, m_variantFactory);

	PE_CCNode * ccnode = new PE_CCNode(m_variantManager);  
	ccnode->init();

	PE_CCLayer * cclayer = new PE_CCLayer(m_variantManager);
	cclayer->init();

	PE_CCLayerColor * cclayercolor = new PE_CCLayerColor(m_variantManager);   
	cclayercolor->init();

	PE_CCMenuItemFont * ccmenuitemfont = new PE_CCMenuItemFont(m_variantManager);   
	ccmenuitemfont->init();
    
	PE_CCMenuItemToggle * ccmenuitemtoggle= new PE_CCMenuItemToggle(m_variantManager);   
	ccmenuitemtoggle->init();

    PE_CCMenuItemSprite * ccmenuitemsprite = new PE_CCMenuItemSprite(m_variantManager);   
	ccmenuitemsprite->init();

	PE_CCLabelTTF * cclabelttf = new PE_CCLabelTTF(m_variantManager);
	cclabelttf->init();
    
	PE_CCSprite * ccsprite = new PE_CCSprite(m_variantManager);
	ccsprite->init();
    
    PE_CCParticleSystem * ccparticlesystem = new PE_CCParticleSystem(m_variantManager);
    ccparticlesystem->init();
    
    QObject::connect(m_variantManager ,SIGNAL(valueChanged(QtProperty*,QVariant)) , this, SLOT(changeValue(QtProperty*,QVariant)) );
}

void PEFactory::changeValue( QtProperty* property ,QVariant variant)
{
	QCocoNode * qnode = CCQTSceneDirector::sharedSceneDirector()->CurrentQNode();
	if (qnode == NULL) return;

	QTreeNode <QCocoNode> * treeNode = CocoHieriachy::sharedHieriachy()->getTreeNodeById(qnode->id);
	QVector<QCocoNode> superNodes =  treeNode->getSupertypes();
	for (int i = 0 ;i < superNodes.size(); i++)
	{
		PEditor * peditor = this->getPEditorByName(superNodes.at(i).name);
		if(peditor ==NULL) continue;
		peditor->valueChanged(property, variant);
	}
	ConstantManager::sharedManager()->setSaved(false);
}

void PEFactory::removeAllQtProperty()
{
	QList <QtProperty *> _properties = m_variantEditor->properties();
	for (int i  =0 ; i < _properties .size() ; i++)
	{
		m_variantEditor->removeProperty(_properties[i]);

		PEditor * editor = getPEditorByName(_properties[i]->propertyName());
		editor->disable();
	}
	
}

PEditor * PEFactory::getPEditorByName(QString name)
{
	PEditor * peditor = m_mapControllers.value(name);
	return peditor;
}

QWidget * PEFactory::createPE( int tid )
{
	removeAllQtProperty();
    QTreeNode <QCocoNode> * treeNode = CocoHieriachy::sharedHieriachy()->getTreeNodeById(tid);
    QVector<QCocoNode> superNodes =  treeNode->getSupertypes();
   
    for (int i = superNodes.size() -1;i >=0 ; i --)
    {
        PEditor * peditor = m_mapControllers.value(superNodes.at(i).name);
		if(peditor == NULL)
			continue;
		peditor->Update();
		
        m_variantEditor->addProperty(peditor->TopItem());
//        editor->checkNode(*node);
//        editor->setVisible(true);
    }
	m_variantEditor->setPropertiesWithoutValueMarked(true);
	m_variantEditor->setRootIsDecorated(false);

	return m_variantEditor; 
}

bool PEFactory::m_bInit = false;

void PEditor::init()
{
	m_topItem = m_variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), name());
	PEFactory::shareFactory()->reg(name(),  this);
	initUI(); 
	
}

 
void PEditor::Update()
{
	this->enable();
	qDebug()<< name() + " refresh";
	if(m_bIsDisplay )
	{
		this->refresh();
	}
}
void PEditor::enable()
{
	m_bIsDisplay = true;
}

void PEditor::disable()
{
	m_bIsDisplay = false;
}

 
